﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlanetInteract : PlanetState
{
    //探索
    public bool CanBeObserve
    {
        get
        {
            return (StateNow.Basic == PlanetState.State.BasicState.UnObserve || StateNow.Basic == PlanetState.State.BasicState.Unknown_Observed);
        }
    }

    public void Observe()//————————————————点可以探索的星系
    {
        if (ControlCenter.Instance.ContactPoint[ControlCenter.Instance.PlayerIdNow - 1] - 1 < 0)
            return;
        ControlCenter.Instance.ContactPoint[ControlCenter.Instance.PlayerIdNow - 1] -= 1;
        if (Owner == 0 && PlanetRealState.Type == PlanetState.RealState.RealType.Empty)
        {
            SoundPlayer.Instance.PlaySound(SoundPlayer.Instance.explore1);
            StateAll[ControlCenter.Instance.PlayerIdNow - 1].Basic = PlanetState.State.BasicState.Observed_empty; //无人星系
            EffectManager.Instance.Create("CirclesWaver", transform.position);
        }
        else
        {
            SoundPlayer.Instance.PlaySound(SoundPlayer.Instance.explore2);
            StateAll[ControlCenter.Instance.PlayerIdNow - 1].Basic = PlanetState.State.BasicState.Observed_Uncontact;//有人星系
            EffectManager.Instance.Create("ScanRespond", transform.position);
        }

        BuyManager.Instance.ShutWindow();
        InitState();
    }

    //交流（源）
    public bool CanBeContact
    {
        get
        {
            return StateNow.Basic == PlanetState.State.BasicState.Observed_Uncontact;
        }
    }

    public void StartContact()//————————————————点可以交流的星系（弹出可以用来交流的雷达站）
    {
        if (ControlCenter.Instance.ContactPoint[ControlCenter.Instance.PlayerIdNow - 1] - 1 < 0)
            return;
        ControlCenter.Instance.ExistsContactTarget = true;
        ControlCenter.Instance.ContactTarget = gameObject;
        ControlCenter.Instance.InitAll();
        BuyManager.Instance.ShutWindow();
    }

    //交流(目标)
    public void Contact()//————————————————点可以用来交流的雷达站 来自发出信号的行星
    {
        ControlCenter.Instance.InitConmunicateBar();
        if (ControlCenter.Instance.ContactPoint[ControlCenter.Instance.PlayerIdNow - 1] - 1 < 0)
            return;
        ControlCenter.Instance.ContactPoint[ControlCenter.Instance.PlayerIdNow - 1] -= 1;
        SoundPlayer.Instance.PlaySound(SoundPlayer.Instance.explore3);
        EffectManager.Instance.Create("ScanStartAndEnd", transform.position, ControlCenter.Instance.ContactTarget.transform.position);

        if (ControlCenter.Instance.ContactTarget.GetComponent<PlanetState>().PlanetRealState.Type == PlanetState.RealState.RealType.OriginCivil && ControlCenter.Instance.ContactTarget.GetComponent<PlanetState>().Owner == 0)
            ControlCenter.Instance.ContactTarget.GetComponent<PlanetState>().Owner = ControlCenter.Instance.PlayerIdNow;//收土著
        else
        {
            expose();//暴露目标
            ControlCenter.Instance.ContactTarget.GetComponent<PlanetState>().StateAll[ControlCenter.Instance.PlayerIdNow - 1].Basic = PlanetState.State.BasicState.UnFriendlyCivil;//对象切换为对自己不友好
        }
        ControlCenter.Instance.ExistsContactTarget = false;
        InitState();
        ControlCenter.Instance.ContactTarget.GetComponent<PlanetState>().InitState();
        ControlCenter.Instance.RefreshPlus();
    }

    private void expose()
    {
        SoundPlayer.Instance.PlaySound(SoundPlayer.Instance.alarm);
        int Owner = ControlCenter.Instance.ContactTarget.GetComponent<PlanetState>().Owner - 1;
        StateAll[Owner].Basic = PlanetState.State.BasicState.UnFriendlyCivil;//自己切换为对对象不友好
    }

    //殖民
    public bool CanBeColonized
    {
        get
        {
            return (GetComponent<PlanetState>().StateNow.Basic == PlanetState.State.BasicState.Observed_empty);
        }
    }

    public void Colonize(int Resource)//————————————————点可以用来殖民的星系
    {
        if (ControlCenter.Instance.Resource[ControlCenter.Instance.PlayerIdNow - 1] - Resource >= 0)
        {
            SoundPlayer.Instance.PlaySound(SoundPlayer.Instance.click1);

            ControlCenter.Instance.Resource[ControlCenter.Instance.PlayerIdNow - 1] -= Resource;
            ControlCenter.Instance.PlanetsCount[ControlCenter.Instance.PlayerIdNow - 1] += 1;
            Owner = ControlCenter.Instance.PlayerIdNow;
            BuyManager.Instance.ShutWindow();

            EffectManager.Instance.Create("Colonize", transform.position);

            InitState();
        }
        else
        {
            ControlCenter.Instance.ShowMessage("Not Enough Resource");
        }
    }

    //建设
    public bool CanBeConsTruct
    {
        get
        {
            return (Owner == ControlCenter.Instance.PlayerIdNow && PlanetRealState.Type != RealState.RealType.OriginCivil);
        }
    }

    public void Construct(int BatteryID, string Type)
    {
        switch (Type)
        {
            case "Factory":
                if (ControlCenter.Instance.Resource[ControlCenter.Instance.PlayerIdNow - 1] - DataCenter.Instance.FactPric < 0)
                {
                    ControlCenter.Instance.ShowMessage("Not Enough Resource");
                    return;
                }            
                SoundPlayer.Instance.PlaySound(SoundPlayer.Instance.click2);
                BuyManager.Instance.ShutWindow();
                Battery[BatteryID].Struct = ColonizeState.StructType.Factory;
                ControlCenter.Instance.Resource[ControlCenter.Instance.PlayerIdNow - 1] -= DataCenter.Instance.FactPric;
                EffectManager.Instance.Create("BuildFactory", transform.position);
                break;

            case "Weapon":
                if (ControlCenter.Instance.Resource[ControlCenter.Instance.PlayerIdNow - 1] - DataCenter.Instance.WeapPric < 0)
                {
                    ControlCenter.Instance.ShowMessage("Not Enough Resource");
                    return;
                }
                SoundPlayer.Instance.PlaySound(SoundPlayer.Instance.click2);
                BuyManager.Instance.ShutWindow();
                Battery[BatteryID].Struct = ColonizeState.StructType.Weapon;
                ControlCenter.Instance.Resource[ControlCenter.Instance.PlayerIdNow - 1] -= DataCenter.Instance.WeapPric;
                EffectManager.Instance.Create("BuildWeapon", transform.position);
                break;

            case "Raidar":
                if (ControlCenter.Instance.Resource[ControlCenter.Instance.PlayerIdNow - 1] - DataCenter.Instance.RaidPric < 0)
                {
                    ControlCenter.Instance.ShowMessage("Not Enough Resource");
                    return;
                }
                SoundPlayer.Instance.PlaySound(SoundPlayer.Instance.click2);
                BuyManager.Instance.ShutWindow();
                Battery[BatteryID].Struct = ColonizeState.StructType.Raider;
                ControlCenter.Instance.Resource[ControlCenter.Instance.PlayerIdNow - 1] -= DataCenter.Instance.RaidPric;
                EffectManager.Instance.Create("BuildRaidar", transform.position);
                break;

            default: break;
        }
        InitState();
        ControlCenter.Instance.RefreshPlus();
    }

    public void Destroyer(int Weapon)
    {
        if (ControlCenter.Instance.Weapon[ControlCenter.Instance.PlayerIdNow - 1] - Weapon >= 0)
        {
            SoundPlayer.Instance.PlaySound(SoundPlayer.Instance.Boom);

            //黑洞
            EffectManager.Instance.Create("BlackHole", transform.position);

            BuyManager.Instance.ShutWindow();
            ControlCenter.Instance.Weapon[ControlCenter.Instance.PlayerIdNow - 1] -= Weapon;
            ControlCenter.Instance.WeaponTimes[ControlCenter.Instance.PlayerIdNow - 1] += 1;
            if (Owner != 0)
                ControlCenter.Instance.PlanetsCount[Owner - 1] -= 1;
            InitState();
            InteractStarManager.Instance.DestroyTimes += 1;
            gameObject.SetActive(false);
        }
        else
        {
            ControlCenter.Instance.ShowMessage("Not Enough Weapon");
        }
    }
}